package com.ad.admin.interview_viewpage;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.ad.admin.util.Globals;
import com.ad.admin.util.QuestionDao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        Globals.init(this);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InputStream is = getAssets().open("question.txt");
                    SharedPreferences sp = getSharedPreferences("question_flag",MODE_PRIVATE);
                    boolean saveFlag = sp.getBoolean("flag",false);
                    if(saveFlag == true){
                        Thread.sleep(2000);
                    }else{
                        QuestionDao.deleteAll();
                        loadInputStream(is);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putBoolean("flag", true);
                        editor.commit();

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent in = new Intent(IndexActivity.this, MainActivity.class);
                startActivity(in);
                finish();
            }
        });
        thread.start();

    }
    public List<Map<String,Object>> loadInputStream(InputStream is) throws IOException {
        List<Map<String, Object>> values = new ArrayList<>();
        BufferedReader br = new BufferedReader(new InputStreamReader(is,"GBK"));
        String line = null;
        StringBuffer question = new StringBuffer();
        StringBuffer answer = new StringBuffer();

        boolean questionFlag = false;
        while((line=br.readLine())!=null) {
            if(line.equals("Start_Question_Flag")) {
                questionFlag  = true;
            }else if(line.equals("Start_Answer_Flag")) {
                questionFlag = false;
            }else if(line.equals("End")) {
                Map<String, Object> map = new HashMap<>();
                map.put("question", question.toString());
                map.put("answer", answer.toString());
                QuestionDao.insertAll(map);
                values.add(map);
                question = new StringBuffer();
                answer = new StringBuffer();
            }else {
                if(questionFlag) {
                    question.append(line);
                }else {
                    answer.append(line);
                }
            }
        }
        return values;
    }
}
